package com.huawei.od;

/**
 * 765. 情侣牵手
 * 贪心算法
 */
public class TestTen {

    public static void main(String[] args) {
        System.out.println(minSwapsCouples(new int[]{0, 3, 1, 2}));
    }

    public static int minSwapsCouples(int[] row) {
        int count = 0;
        for (int i = 0; i < row.length; i += 2) {
            int x = row[i];
            // 异或运算
            if (row[i + 1] == (x ^ 1)) {
                continue;
            }
            count++;
            for (int j = i + 1; j < row.length; ++j) {
                if (row[j] == (x ^ 1)) {
                    // 交换位置
                    row[j] = row[i + 1];
                    row[i + 1] = x ^ 1;
                    break;
                }
            }
        }
        return count;
    }

}
